//
//  PlaceholderView.m
//  PlaceholderView
//
//  Created by Burn on 2019/4/9.
//  Copyright © 2019 menksoft. All rights reserved.
//
//  重播
#define kReplay @"\n"
//  正在加载 前面加了4个空格
#define kLoading @"         "
//  即将播放
#define kPlaySoon @" \n"
//  正在使用移动数据
#define KMobileNetwork @"\n\n\n"
//  无网络,设置网络,重试
#define kSETINTERNET @"\n\n\n \n \n\n "
//  正在使用非wifi网络
#define kNOWIFI @"W I F I\n\n \n\n"
//  重试
#define kTRYAGAIN @" \n"
//  继续播放
#define kJIXUBOFANG @" \n"
//  网络错误
#define kINTERNETERROR @" "


//#define kFontName  @"MenksoftQagan_mirror"

#import "PlaceholderView.h"
#import "Macro.h"

@interface PlaceholderView()

@property(strong,nonatomic) UIImageView *repetePlay;

@property(strong,nonatomic) UIActivityIndicatorView *progressView;

@property(strong,nonatomic) UILabel *messageLabel;

@property(strong,nonatomic) UILabel *willBePlay;

@property(strong,nonatomic) UILabel *errorInterNet;

@property(strong,nonatomic) UILabel *checkInternetL;
@property(strong,nonatomic) UIButton *tryAgain;

@property(strong,nonatomic) UILabel *phoneDataL;
@property(strong,nonatomic) UIButton *phoneDataBtn;

@end

@implementation PlaceholderView

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self setupSubViews];
        [self addtarget];
        
    }
    return self;
}

- (void)holder{
    self.errorInterNet.hidden = YES;
    self.repetePlay.hidden = YES;
    self.messageLabel.hidden = NO;
    self.willBePlay.hidden = YES;
    self.checkInternetL.hidden = YES;
    self.tryAgain.hidden = YES;
    
    self.phoneDataL.hidden = YES;
    self.phoneDataBtn.hidden = YES;
}

- (void)wait{
    self.errorInterNet.hidden = YES;
    self.repetePlay.hidden = YES;
    self.messageLabel.hidden = YES;
    self.willBePlay.hidden = NO;
    self.checkInternetL.hidden = YES;
    self.tryAgain.hidden = YES;
    
    self.phoneDataL.hidden = YES;
    self.phoneDataBtn.hidden = YES;
}
- (void)replay{
    self.errorInterNet.hidden = YES;
    self.repetePlay.hidden = NO;
    self.messageLabel.hidden = YES;
    self.willBePlay.hidden = YES;
    self.checkInternetL.hidden = YES;
    self.tryAgain.hidden = YES;
    
    self.phoneDataL.hidden = YES;
    self.phoneDataBtn.hidden = YES;
}

- (void)checkInternet{
    self.errorInterNet.hidden = YES;
    self.repetePlay.hidden = YES;
    self.messageLabel.hidden = YES;
    self.willBePlay.hidden = YES;
    self.checkInternetL.hidden = NO;
    self.tryAgain.hidden = NO;
    
    self.phoneDataL.hidden = YES;
    self.phoneDataBtn.hidden = YES;
}
- (void)phoneData{
    self.errorInterNet.hidden = YES;
    self.repetePlay.hidden = YES;
    self.messageLabel.hidden = YES;
    self.willBePlay.hidden = YES;
    self.checkInternetL.hidden = YES;
    self.tryAgain.hidden = YES;
    self.phoneDataL.hidden = NO;
    self.phoneDataBtn.hidden = NO;
    
}

- (void)interNetError{
    self.repetePlay.hidden = YES;
    self.messageLabel.hidden = YES;
    self.willBePlay.hidden = YES;
    self.checkInternetL.hidden = YES;
    self.tryAgain.hidden = YES;
    self.phoneDataL.hidden = YES;
    self.phoneDataBtn.hidden = YES;
    
    self.errorInterNet.hidden = NO;
}

- (void)setupSubViews{
    self.repetePlay = [[UIImageView alloc]init];
    self.repetePlay.transform = CGAffineTransformScale(self.repetePlay.transform, -1, 1);
    self.repetePlay.transform =  CGAffineTransformRotate(self.repetePlay.transform, M_PI / 2);
    
    self.repetePlay.layer.cornerRadius = 15;
    self.repetePlay.image = [UIImage imageNamed:@"play_rePlay_full_17x17_"];
    self.repetePlay.hidden = YES;
    self.repetePlay.userInteractionEnabled = YES;
    UITapGestureRecognizer *labelTapGestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(replayClick)];
    [self.repetePlay addGestureRecognizer:labelTapGestureRecognizer];

    [self addSubview:self.repetePlay];
    
    self.messageLabel = [[UILabel alloc]init];
    self.messageLabel.transform = CGAffineTransformScale(self.messageLabel.transform, -1, 1);
    self.messageLabel.transform =  CGAffineTransformRotate(self.messageLabel.transform, M_PI / 2);
    self.messageLabel.font = [UIFont fontWithName:kFontName size:17];
    self.messageLabel.textAlignment = NSTextAlignmentCenter;
    self.messageLabel.backgroundColor = [UIColor blackColor];
    self.messageLabel.textColor = [UIColor whiteColor];
    self.messageLabel.numberOfLines = 1;
    self.messageLabel.layer.cornerRadius = 20;
    self.messageLabel.layer.masksToBounds = YES;
    self.messageLabel.text = kLoading;
    [self addSubview:self.messageLabel];
    
    
    [self.progressView startAnimating];
    [self.messageLabel addSubview:self.progressView];
    self.progressView.frame = CGRectMake(-8, 0, 44, 44);
    self.messageLabel.hidden = YES;

    
    self.willBePlay = [[UILabel alloc]init];
    self.willBePlay.transform = CGAffineTransformScale(self.willBePlay.transform, -1, 1);
    self.willBePlay.transform =  CGAffineTransformRotate(self.willBePlay.transform, M_PI / 2);
    self.willBePlay.font = [UIFont fontWithName:kFontName size:17];
    self.willBePlay.textAlignment = NSTextAlignmentCenter;
    self.willBePlay.backgroundColor = [UIColor blackColor];
    self.willBePlay.textColor = [UIColor whiteColor];
    self.willBePlay.numberOfLines = 0;
    self.willBePlay.layer.cornerRadius = 19;
    self.willBePlay.layer.masksToBounds = YES;
    self.willBePlay.text = kPlaySoon;
    
    [self addSubview:self.willBePlay];
    self.willBePlay.hidden = YES;
    
    
    self.checkInternetL = [[UILabel alloc]init];
    self.checkInternetL.transform = CGAffineTransformScale(self.checkInternetL.transform, -1, 1);
    self.checkInternetL.transform =  CGAffineTransformRotate(self.checkInternetL.transform, M_PI / 2);
    self.checkInternetL.font = [UIFont fontWithName:kFontName size:17];
    self.checkInternetL.textAlignment = NSTextAlignmentCenter;
    self.checkInternetL.backgroundColor = [UIColor blackColor];
    self.checkInternetL.textColor = [UIColor whiteColor];
    self.checkInternetL.numberOfLines = 0;
    self.checkInternetL.text = kSETINTERNET;
    [self addSubview:self.checkInternetL];
    
    
    self.checkInternetL.backgroundColor = [UIColor clearColor];
    self.checkInternetL.hidden = YES;
    
    CGFloat hafW = [UIImage imageNamed:@"play_rePlay_full_17x17_"].size.width * .5;
    CGFloat w = 90;
    self.tryAgain = [UIButton buttonWithType:UIButtonTypeCustom];
    self.tryAgain.transform = CGAffineTransformScale(self.tryAgain.transform, -1, 1);
    self.tryAgain.transform =  CGAffineTransformRotate(self.tryAgain.transform, M_PI / 2);
    
    [self.tryAgain setImageEdgeInsets:UIEdgeInsetsMake( -w * .5 - 5, hafW + 3, 0, 0)];
    
    [self.tryAgain setTitleEdgeInsets:UIEdgeInsetsMake(hafW, -15, 0, 0)];
    [self.tryAgain setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.tryAgain setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];
    self.tryAgain.titleLabel.font = [UIFont fontWithName:kFontName size:17];
    self.tryAgain.titleLabel.numberOfLines = 0;
    [self.tryAgain setImage:[UIImage imageNamed:@"play_rePlay_full_17x17_"] forState:UIControlStateNormal];
//    self.tryAgain.backgroundColor = RGBACOLOR(33, 188, 34, 1);
    self.tryAgain.layer.borderWidth = 1;
    self.tryAgain.layer.borderColor = RGBACOLOR(33, 188, 34, 1).CGColor;
    [self.tryAgain setTitle:kTRYAGAIN forState:UIControlStateNormal];
    self.tryAgain.layer.cornerRadius = 20;
    self.tryAgain.layer.masksToBounds = YES;
    
    [self addSubview:self.tryAgain];
    self.tryAgain.hidden = YES;
    
    
    
    self.phoneDataL = [[UILabel alloc]init];
    self.phoneDataL.backgroundColor = [UIColor clearColor];
    self.phoneDataL.transform = CGAffineTransformScale(self.phoneDataL.transform, -1, 1);
    self.phoneDataL.transform =  CGAffineTransformRotate(self.phoneDataL.transform, M_PI / 2);
    self.phoneDataL.font = [UIFont fontWithName:kFontName size:17];
    self.phoneDataL.textAlignment = NSTextAlignmentCenter;
    self.phoneDataL.textColor = [UIColor whiteColor];
    self.phoneDataL.numberOfLines = 0;
    self.phoneDataL.text = kNOWIFI;
    [self addSubview:self.phoneDataL];
    
    self.phoneDataL.hidden = YES;
    [self addSubview:self.phoneDataL];
    
    self.phoneDataBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.phoneDataBtn.transform = CGAffineTransformScale(self.phoneDataBtn.transform, -1, 1);
    self.phoneDataBtn.transform =  CGAffineTransformRotate(self.phoneDataBtn.transform, M_PI / 2);
    self.phoneDataBtn.titleLabel.font = [UIFont fontWithName:kFontName size:15];
    self.phoneDataBtn.titleLabel.numberOfLines = 0;
    [self.phoneDataBtn setTitle:kJIXUBOFANG forState:UIControlStateNormal];
    
    self.phoneDataBtn.hidden = YES;
    self.phoneDataBtn.layer.borderWidth = 1;
    self.phoneDataBtn.layer.cornerRadius = 20;
    self.phoneDataBtn.layer.masksToBounds = YES;
    self.phoneDataBtn.layer.borderColor = RGBACOLOR(33, 188, 34, 1).CGColor;
    [self addSubview:self.phoneDataBtn];
    
    
    self.errorInterNet = [[UILabel alloc]init];
    self.errorInterNet.transform = CGAffineTransformScale(self.errorInterNet.transform, -1, 1);
    self.errorInterNet.transform =  CGAffineTransformRotate(self.errorInterNet.transform, M_PI / 2);
    self.errorInterNet.font = [UIFont fontWithName:kFontName size:17];
    self.errorInterNet.textAlignment = NSTextAlignmentCenter;
    self.errorInterNet.backgroundColor = [UIColor blackColor];
    self.errorInterNet.textColor = [UIColor whiteColor];
    self.errorInterNet.numberOfLines = 1;
    self.errorInterNet.layer.cornerRadius = 20;
    self.errorInterNet.layer.masksToBounds = YES;
    self.errorInterNet.text = kINTERNETERROR;
    [self addSubview:self.errorInterNet];
    self.errorInterNet.hidden = YES;
    
    self.messageLabel.alpha = .8;
    self.repetePlay.alpha = .8;
    self.willBePlay.alpha = .8;
    self.checkInternetL.alpha = .8;
    self.errorInterNet.alpha = .8;
    
}

- (void)layoutSubviews{
    [super layoutSubviews];
    CGFloat w = 90;
    CGFloat h = 40;
    self.repetePlay.frame = CGRectMake(self.bounds.size.width * .5 - 20,self.bounds.size.height * .5 - 25 , 40, 40);
    self.messageLabel.frame = CGRectMake(self.bounds.size.width * .5 - 22,0, 44, self.frame.size.height);
    self.willBePlay.frame = CGRectMake(self.bounds.size.width * .5 - 20,self.bounds.size.height * .5 - 42.5 , 40, 85);
    self.checkInternetL.frame = CGRectMake(self.bounds.size.width * .5 - 70,self.bounds.size.height * .5 - 60 , 140, 70);
    self.tryAgain.frame = CGRectMake(self.bounds.size.width * .5 - 50, CGRectGetMaxY(self.checkInternetL.frame) + 5, w, h);
    self.phoneDataL.frame = CGRectMake(self.bounds.size.width * .5 - 70,self.bounds.size.height * .5 - 60 , 140, 70);
    self.phoneDataBtn.frame = CGRectMake(self.bounds.size.width * .5 - 50, CGRectGetMaxY(self.phoneDataL.frame) + 5, w, h + 5);
    self.errorInterNet.frame = CGRectMake(self.bounds.size.width * .5 - 22,self.bounds.size.height * .5 - 60 , 44, 120);
}

- (void)addtarget{
    self.repetePlay.userInteractionEnabled = YES;
    UITapGestureRecognizer *labelTapGestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(repteBtn)];
    [self.repetePlay addGestureRecognizer:labelTapGestureRecognizer];
    
    [self.tryAgain addTarget:self action:@selector(tryAgainInternet) forControlEvents:UIControlEventTouchUpInside];

    [self.phoneDataBtn addTarget:self action:@selector(resume) forControlEvents:UIControlEventTouchUpInside];

}
//  重播
- (void)repteBtn{
    if ([self.delegate respondsToSelector:@selector(replayClick)]) {
        [self.delegate replayClick];
    }
}
//  重试
- (void)tryAgainInternet{
    if ([self.delegate respondsToSelector:@selector(tryAgainClick)]) {
        [self.delegate tryAgainClick];
    }
}

//  继续
- (void)resume{
    if ([self.delegate respondsToSelector:@selector(resume)]) {
        [self.delegate resumeClick];
    }
}

- (UIActivityIndicatorView *)progressView{
    if (!_progressView) {
        _progressView = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleMedium];
        _progressView.transform = CGAffineTransformScale(_progressView.transform, -1, 1);
        _progressView.transform =  CGAffineTransformRotate(_progressView.transform, M_PI / 2);
    }
    return _progressView;
}



@end
